package com.pys.service;

import java.util.List;

import org.hibernate.Session;

import com.pys.dao.impl.TalepDAO;
import com.pys.pojo.PysKullanici;
import com.pys.pojo.PysTalep;
import com.pys.utils.HibernateUtil;

public class TalepService {

	public PysTalep getById(long id) {
		Session session = null;
		PysTalep pysTalep = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			TalepDAO talepDAO = new TalepDAO(session);
			pysTalep = (PysTalep) talepDAO.findById(PysTalep.class, id);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return pysTalep;
	}

	public void save(PysTalep pysTalep) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			TalepDAO talepDAO = new TalepDAO(session);
			talepDAO.save(pysTalep);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public void update(PysTalep pysTalep) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			TalepDAO talepDAO = new TalepDAO(session);
			talepDAO.update(pysTalep);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	// public void roleEkle(String roleAd) {
	// Session session = HibernateUtil.getSessionFactory().openSession();
	// session.beginTransaction();
	// TalepDAO talepDAO = new TalepDAOImpl(session);
	// PysTalep pysTalep = new PysTalep();
	// pysTalep.setAd(roleAd);
	// talepDAO.save(pysTalep);
	// session.getTransaction().commit();
	// session.close();
	// }

	public List<PysTalep> getAllList(Boolean deleted) {
		Session session = null;
		List<PysTalep> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			TalepDAO talepDAO = new TalepDAO(session);
			if (deleted == null)
				list = talepDAO.findAll(PysTalep.class);
			else
				list = talepDAO.findByProperty(PysTalep.class, "deleted",
						deleted);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

	public void delete(PysTalep pysTalep) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			TalepDAO talepDAO = new TalepDAO(session);
			talepDAO.delete(pysTalep);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public List<PysTalep> getByName(String name) {
		Session session = null;
		List<PysTalep> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			TalepDAO talepDAO = new TalepDAO(session);
			list = talepDAO.findByProperty(PysTalep.class, "name", name);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

	public List<PysTalep> getByKullanici(PysKullanici kullanici) {
		Session session = null;
		List<PysTalep> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			TalepDAO talepDAO = new TalepDAO(session);
			list = talepDAO.findByProperty(PysTalep.class,
					"pysKullaniciByAtananKullaniciId", kullanici);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

	public List<PysTalep> search(PysTalep pysTalep) {
		Session session = null;
		List<PysTalep> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			TalepDAO talepDAO = new TalepDAO(session);
			list = talepDAO.search(pysTalep);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

}
